#! /bin/sh
# PCP QA Test No. 657
# checks pmdadm functionality
# Requires dmsetup utility to be installed, else the test will
# not run (PMDA will fail to install).
#
# Copyright (c) 2015 Red Hat, Inc. All Rights Reserved.
#
seq=`basename $0`

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

echo "QA output created by $seq"

[ $PCP_PLATFORM = linux ] || _notrun "Device Mapper is Linux-specific (dm)"
[ -d $PCP_PMDAS_DIR/dm ] || _notrun "dm PMDA directory is not installed"
[ -x $PCP_PMDAS_DIR/dm/pmdadm ] || _notrun "pcp-pmda-dm package not installed"
grep -qs "without support for dmstats" $PCP_PMDAS_DIR/dm/pmdadm && _notrun "PMDA not built with dmstats support"
which dmsetup >/dev/null 2>&1 || _notrun "dmsetup from device-mapper package is not installed"

root=$tmp.root
export DM_SETUP_CACHE="cat $root/dmcache-caches"
export DM_SETUP_THINPOOL="cat $root/dmthin-pool"
export DM_SETUP_THIN="cat $root/dmthin-thin"
export DM_SETUP_CRYPT="cat $root/dmsetup-crypt"
export DM_SETUP_CRYPTSETUP="cat $root/cryptsetup-status"
export DM_SETUP_MULTIPATH="cat $root/multipathd"

# don't want Install to possibly restart pmcd via systemctl, as this
# will clobber the environment, ...
#
if ! _service pmlogger stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmlogger_end || _exit 1
if ! _service pmcd stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmcd_end || _exit 1
if [ "$PCPQA_SYSTEMD" = x -o "${PCPQA_SYSTEMD-x}" != x ]
then
    # $PCPQA_SYSTEMD is already set ...
    save_pcpqa_systemd="$PCPQA_SYSTEMD"
    restore_pcpqa_systemd=true
else
    restore_pcpqa_systemd=false
fi
export PCPQA_SYSTEMD=no

# helper functions

_install_pmda()
{
    # install the PMDA
    # Note: warnings from dmstats install are optional, so filter 'em out
    # Note: VDO stats are exercised via separate tests, so filter 'em out
    #
    cd $PCP_PMDAS_DIR/dm
    $sudo ./Remove < /dev/null >/dev/null 2>&1
    $sudo ./Install < /dev/null >$tmp.out 2>&1
    cat $tmp.out \
    | _filter_pmda_install \
    | sed \
	-e '/.*pmcd.*/d' \
	-e '/.*pmlogger.*/d' \
	-e '/^NOTE:.*/d' \
	-e '/Latest.*/d' \
	-e '/Duplicate.*/d' \
	-e '/^See also.*/d' \
	-e '/^Check vdo metrics.*/d' \
	-e '/^To use the dmstats metrics.*/d' \
	-e '/^Check dmstats metrics/s/[0-9][0-9]* warnings, //' \
	-e 's/[0-9]* values/X values/' \
    | tr -s '\n'
}

_remove_pmda()
{
    cd $PCP_PMDAS_DIR/dm
    $sudo ./Remove < /dev/null > /dev/null 2>&1
}

_cleanup()
{
    if $restore_pcpqa_systemd
    then
	export PCPQA_SYSTEMD="$save_pcpqa_systemd"
    else
	unset PCPQA_SYSTEMD
    fi
    _restore_pmda_install dm
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    $sudo rm -fr $root
    $sudo rm -fr $tmp.*.dir
    $sudo rm -f $tmp.*
    exit $status
}

status=1 # failure is the default!
trap "_cleanup" 0 1 2 3 15
_stop_auto_restart pmcd

$sudo rm -fr $root
$sudo rm -fr $tmp.*.dir
touch $seq_full

# real QA test starts here
cd $here

echo
echo "=== Setting up QA fakeroot testing data ==="
$sudo rm -fr $root
mkdir $root || _fail "root in use when processing fakeroot"
cd $root
echo 'zz_cache: 0 1677721600 cache 8 10162/262144 128 39839/3276800 1087840 821795 116320 2057235 0 39835 0 1 writeback 2 migration_threshold 2048 mq 10 random_threshold 4 sequential_threshold 0 discard_promote_adjustment 1 read_promote_adjustment 0 write_promote_adjustment 0' > $root/dmcache-caches
echo 'yy_pool: 0 409600 thin-pool 0 13/65536 0/3200 - rw no_discard_passdown queue_if_no_space' > $root/dmthin-pool
echo 'vg_1-lv3: 0 8388608 thin 7000704 8388607
vg_1-lv2: 0 8388608 thin 6832768 8388607' > $root/dmthin-thin
echo 'luks-4456fe89-d09e-416f-8721-bedb66abda85: 0 996853760 crypt' > $root/dmsetup-crypt
echo '/dev/mapper/luks-4456fe89-d09e-416f-8721-bedb66abda85 is disabled and is not in use.
  type:    LUKS2
  cipher:  aes-xts-plain64
  keysize: 512 [bits]
  key location: keyring
  device:  /dev/nvme0n1p3
  sector size:  512 [bytes]
  offset:  32768 [512-byte units] (16777216 [bytes])
  size:    996853760 [512-byte units] (510389125120 [bytes])
  mode:    read/write
  flags:   discards' > $root/cryptsetup-status

cat << 'MULTIPATHD_OUTPUT' > $root/multipathd
mpatha (3600c0ff000123456789012345000000) dm-1 HITACHI,DF400F
size=500G features='1 queue_if_no_path' hwhandler='0' wp=rw
|-+- policy='round-robin 0' prio=50 status=active
| |- 1:0:0:1 sda 8:0   active ready running
| |- 2:0:0:1 sdc 8:32  active ready running
| |- 3:0:0:1 sde 8:64  active ready running
| `- 4:0:0:1 sdg 8:96  active ready running
`-+- policy='round-robin 0' prio=10 status=enabled
  |- 1:0:1:1 sdb 8:16  active ready running
  |- 2:0:1:1 sdd 8:48  active ready running
  |- 3:0:1:1 sdf 8:80  active ready running
  `- 4:0:1:1 sdh 8:112 active ready running

mpathb (36005076303ffc56200000000000010aa) dm-4 Pure Storage,FlashArray
size=1.5T features='3 queue_if_no_path pg_init_retries 50' hwhandler='1 alua' wp=rw
|-+- policy='service-time 0' prio=100 status=active
| |- 5:0:0:1 sdi 8:128 active ready running
| `- 6:0:0:1 sdk 8:160 active ready running
`-+- policy='service-time 0' prio=10 status=enabled
  |- 5:0:1:1 sdj 8:144 failed faulty detached
  `- 6:0:1:1 sdl 8:176 active ready running"
MULTIPATHD_OUTPUT

cd $here

echo
echo "=== Installing Device Mapper PMDA ==="
_prepare_pmda_install dm || _exit 1
_install_pmda

echo
echo "=== Check dm-cache metrics ==="
pminfo -dfmtT dmcache

echo
echo "=== Check dm-thin metrics for thin pool metrics ==="
pminfo -dfmtT dmthin.pool

echo
echo "=== Check dm-thin metrics for thin vol metrics ==="
pminfo -dfmtT dmthin.vol

echo
echo "=== Check dm-crypt metrics for crypt metrics ==="
pminfo -dfmtT dmcrypt

echo
echo "=== Check dm-multipath metrics ==="
pminfo -dfmtT dmmultipath

# cleanup
_remove_pmda

status=0
exit
